<?php
$this->load->view('admin/layout/head', array('title'=>"商品管理",'nav'=>'product','css'=>array(
	'/assets/ueditor/themes/default/css/ueditor.min.css',
	'/assets/select2/css/select2.min.css',
	'/assets/jedate/skin/jedate.css')), FALSE);
switch ($this->session->admin_language) {
	case 'chinese':
		$prefix = '';
		break;
	case 'english':
		$prefix = 'en_';
		break;
	default:
		$prefix = '';
		break;
}

?>

<div id="container">
<ul class="nav nav-tabs" id="subject-tab">
	<li class="active"><a href="javascript:;"><?php echo isset($product)? $this->lang->line('edit_product') : $this->lang->line('create_product') ?></a></li>
</ul>
<form class="form-inline deal-from container-fluid no-padding" id="product-form">
	<input type="hidden" name="id" value="<?php echo isset($product['id']) ? $product['id'] : 0 ?>">
	<div class="mt-20">
		<label for=""><?= $this->lang->line('title') ?></label>
		<div class="form-group">
			<input type="text" name="title" class="form-control" value="<?= set_value('title', empty($product['title']) ? '' : $product['title']) ?>" style="width: 400px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('en_product_title') ?></label>
		<div class="form-group">
			<input type="text" name="en_title" class="form-control" value="<?= set_value('en_title', empty($product['en_title']) ? '' : $product['en_title']) ?>" style="width: 400px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('category and brand') ?></label>
		<div class="form-group">
			<select name="category_id" id="category_id_slt" class="form-control">
		  		<option value=""><?= $this->lang->line('category') ?></option>
		  		<?php foreach($categorys as $cate): ?>
		  		<option value="<?php echo $cate['id']?>" <?php if(isset($product) && $product['category_id'] == $cate['id']) echo 'selected' ?>><?php echo $cate[$prefix.'title'] ?></option>
		  		<?php endforeach; ?>
		  	</select>
		  	<select name="category_id2" id="category_id2_slt" class="form-control">
		  		<option value=""><?= $this->lang->line('sub_categories') ?></option>
		  		<?php foreach($categorys2 as $cate): ?>
		  		<option value="<?php echo $cate['id']?>" <?php if(isset($product) && $product['category_id2'] == $cate['id']) echo 'selected' ?>><?php echo $cate[$prefix.'title'] ?></option>
		  		<?php endforeach; ?>
		  	</select>
		  	<select name="brand_id" class="form-control brand-slt">
		  		<option value=""><?= $this->lang->line('choose_brand') ?></option>
		  		<?php foreach($brands as $b): ?>
		  		<option value="<?php echo $b['id']?>" <?php if(isset($product) && $product['brand_id'] == $b['id']) echo 'selected' ?>><?php echo $b[$prefix.'title'] ?></option>
		  		<?php endforeach; ?>
		  	</select>
		</div>
	</div>

	<div class="mt-20">
		<label for=""><?= $this->lang->line('product attributes') ?></label>
		<div class="form-group">
			<select name="status" id="status" class="form-control">
				<option value="1"><?= $this->lang->line('put on shelves') ?></option>
				<option value="0" <?php if(isset($product) && !$product['status']) echo 'selected' ?> ><?= $this->lang->line('put off shelves') ?></option>
			</select>
			<input type="checkbox" name="is_recommend" value="1" <?= isset($product['is_recommend']) && $product['is_recommend'] == 1 ? 'checked' : '' ?>> <?= $this->lang->line('recommend') ?>
			<input type="checkbox" name="is_new" value="1" <?= isset($product['is_new']) && $product['is_new'] == 1 ? 'checked' : '' ?>> <?= $this->lang->line('new_product') ?>
			<input type="checkbox" name="is_hot" value="1" <?= isset($product['is_hot']) && $product['is_hot'] == 1 ? 'checked' : '' ?>> <?= $this->lang->line('hot_product') ?>
			<input type="checkbox" name="is_weight" value="1" <?= isset($product['is_weight']) && $product['is_weight'] == 1 ? 'checked' : '' ?>> <?= $this->lang->line('weight_product') ?>
			<input type="checkbox" name="is_cashier_hot" value="1" <?= isset($product['is_cashier_hot']) && $product['is_cashier_hot'] == 1 ? 'checked' : '' ?>> <?= $this->lang->line('cashier_hot_product') ?>
			<?= $this->lang->line('sort') ?>：<input type="text" name="sort" class="form-control" value="<?= set_value('sort', empty($product['sort']) ? '' : $product['sort']) ?>" style="width: 60px">
			<?= $this->lang->line('hits') ?>：<input type="text" name="hits" class="form-control" value="<?= set_value('hits', empty($product['hits']) ? '' : $product['hits']) ?>" style="width: 60px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('thumbnail image') ?></label>
		<div class="form-group" >
			<a href="javascript:;" class="btn btn-primary" id='up-btn1'><?= $this->lang->line('choose_image') ?></a>
			<input type="hidden" name='thumbnail' id="thumbnail-image"  value="<?= set_value('thumbnail', empty($product['thumbnail']) ? '' : $product['thumbnail']) ?>">
			<div id="up-container1">
				<?php if(set_value('thumbnail', empty($product['thumbnail']) ? '' : $product['thumbnail'])): ?>
				<img src="<?= set_value('thumbnail', empty($product['thumbnail']) ? '' : $product['thumbnail']) ?>" alt="" style="max-width: 100px; max-height: 100px"/>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('image') ?></label>
		<div class="form-group" >
			<a href="javascript:;" class="btn btn-primary" id='up-btn2'><?= $this->lang->line('choose_image') ?></a>
			<div id="up-container2">
				<?php if(isset($product) && $product['images']): ?>
				<?php $images = explode(',',$product['images']); ?>
				<?php foreach ($images as $img):?>
					<div class="images-item">
						<input type="hidden" name='images[]'  value="<?php echo $img ?>">
						<img src="<?php echo $img?>" alt=""/>
						<a href="javascript:;">×</a>
					</div>
				<?php endforeach; ?>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('product_detail') ?></label>
		<div class="form-group" >
			<textarea id="editor" name="content" style="width:750px; height:400px;"><?= set_value('content', empty($product['content']) ? '' : $product['content']) ?></textarea>
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('en_product_detail') ?></label>
		<div class="form-group" >
			<textarea id="editor1" name="en_content" style="width:750px; height:400px;"><?= set_value('en_content', empty($product['en_content']) ? '' : $product['en_content']) ?></textarea>
		</div>
	</div>

	<div class="mt-20">
		<label for=""><?= $this->lang->line('unit') ?></label>
		<div class="form-group" >
			<select name="unit" id="unit" class="form-control">
				<?php foreach ($specs as $key => $value): ?>
					<option value="<?= $value['id'] ?>" <?= set_value('unit', isset($product['unit']) ? $product['unit'] : '') == $value['id'] ? 'selected' : '' ?>><?= $value[$prefix.'title'] ?></option>
				<?php endforeach ?>
			</select>
			<input type="text" name="add_unit" class="form-control" style="width:100px" placeholder="新增单位名称">
			<input type="text" name="add_unit_en" class="form-control" style="width:100px" placeholder="Unit Name">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?= $this->lang->line('about recommend') ?></label>
		<div class="form-group">
			<div class="dropdown">
				<input type="text" name="product-name" class="form-control" value=""> <a href="javascript:;" class="btn btn-success product-recommends-btn">搜索</a>
				<ul class="dropdown-menu product-recommends-slt"></ul>
			</div>

			<div class="recommends">
				<?php foreach (isset($recommends) ? $recommends : [] as $key => $value): ?>
					<div>
						<input type="checkbox" class="product-<?= $value['id'] ?>" name="recommends[]" value="<?= $value['id'] ?>" checked/> <?= $value[$prefix.'title'] ?>
					</div>
				<?php endforeach ?>
			</div>

		</div>
	</div>

	<div class="mt-20">
		<label for=""><?= $this->lang->line('supplier') ?></label>
		<div class="form-group" >
			<select name="supplier_id" class="form-control">
				<option value=""><?= $this->lang->line('choose') ?></option>
				<?php foreach ($supplier as $key => $value): ?>
					<option value="<?= $value['id'] ?>" <?= set_value('supplier_id', isset($product['supplier_id']) ? $product['supplier_id'] : '') == $value['id'] ? 'selected' : '' ?>><?= $value['title'] ?></option>
				<?php endforeach ?>
			</select>
		</div>
	</div>


	<div class="mt-20">
		<label for=""><?= $this->lang->line('product_spec') ?></label>
		<div class="form-group" >
			<a href="javascript:;" class="btn btn-primary" id='item-add'><?= $this->lang->line('create_spec') ?></a>
			<div>
				<table class="table table-bordered table-striped table-hover item-table">
					<thead >
						<tr>
							<th width="100"><?= $this->lang->line('spec_title') ?></th>
							<th width="100"><?= $this->lang->line('en_spec_title') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('original_price') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('sell_price') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('tax') ?>(1/6)</th>
							<th width="80" class="text-center"><?= $this->lang->line('barcode content') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('stock') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('this stock expire') ?></th>
							<th class="text-center"><?= $this->lang->line('not enough') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('weight') ?></th>
							<th width="80" class="text-center"><?= $this->lang->line('sales') ?></th>
							<th width="60" class="text-center"><?= $this->lang->line('actions') ?></th>
						</tr>
					</thead>
					<tbody>
						<?php $i = 1; ?>
						<?php if(isset($items) && $items): ?>
						<?php foreach ($items as $item):?>
						<tr>
							<td>
							<input type="hidden" name="item[id][]" value="<?php echo $item['id'] ?>">
							<input type="text" class="form-control" style="width: 100px;" name="item[title][]" value="<?php echo $item['title']?>"></td>
							<td>
							<input type="text" class="form-control" style="width: 100px;" name="item[en_title][]" value="<?php echo $item['en_title']?>"></td>
							<td><input type="text" class="form-control original_price" style="width: 80px;" name="item[original_price][]" value="<?php echo $item['original_price']?>"></td>
							<td>
								<input type="number" name="item[price][]" class="form-control price" style="width: 80px;" value="<?= $item['price'] ?>">
							</td>
							<td style="text-align:center">
								<input type="hidden" name="item[tax][]" value="<?php echo $item['tax']>0?1:0 ?>">
								<i class="glyphicon glyphicon-ok tax-toggle <?php echo $item['tax']>0?'active':'' ?>"></i>
								<span><?php echo round($item['original_price']*0.2,2) ?></span>
							</td>
							<td>
								<input type="text" name="item[barcode][]" class="form-control" style="width: 120px;" value="<?= $item['barcode'] ?>">
							</td>
							<td>
								<input type="text" name="item[stock][]" class="form-control" value="<?= $item['stock'] ?>">
								<input type="hidden" name="item[last_stock][]" class="form-control" value="<?= $item['stock'] ?>">
							</td>
							<td>
								<input type="text" name="item[expire][]" class="date form-control" value="<?= (new DateTime())->add(new DateInterval('P2D'))->format('Y-m-d H:i:s') ?>">
							</td>
							<td>
								<input type="text" class="form-control" style="width: 80px;" name="item[danger_stock][]" value="<?= $item['danger_stock'] ?>">
							</td>
							<td><input type="text" class="form-control" style="width: 80px;" name="item[weight][]" value="<?php echo $item['weight']?>"></td>
							<td class="text-center"><?php echo $item['solded'] ?></td>
							<td class="text-center"><a href="javascript:;" class="item-del" item_id="<?php echo $item['id'] ?>"><?= $this->lang->line('delete') ?></a></td>
						</tr>
						<?php $i++; ?>
						<?php endforeach; ?>
						<?php endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>




	<div style="padding-left:110px" class="mt-20">
      <a href="javascript:;" class="btn btn-primary save"><?= $this->lang->line('save') ?></a>
      <a href="<?php echo $back_url ?>" class="btn btn-default cancel"><?= $this->lang->line('cancel') ?></a>
    </div>


</form>
</div>


<?php
// $js_array = array(
// 	'/assets/umeditor/third-party/template.min.js',
// 	'/assets/umeditor/umeditor.config.js',
// 	'/assets/umeditor/umeditor.js',
// 	'/assets/umeditor/lang/zh-cn/zh-cn.js',
// 	'/assets/plupload/plupload.full.min.js'
// 	);
$js_array = array(
	'/assets/plupload/plupload.full.min.js',
	'/assets/ueditor/ueditor.config.js',
	'/assets/ueditor/ueditor.all.min.js',
	'/assets/ueditor/lang/zh-cn/zh-cn.js',
	'/assets/select2/js/select2.full.min.js',
	'/assets/jedate/jedate.min.js',
);
$this->load->view('admin/layout/footer',array('js'=>$js_array));
?>
<script type="text/javascript">
	var ue = UE.getEditor('editor');
	var ue1 = UE.getEditor('editor1');

$(function(){
	datepicker('.date','<?php echo $this->session->admin_language ?>');
	$('.brand-slt').select2();

	uploadImg('up-btn1',function(img){
		$('#up-container1').html('<img src="'+img+'" style="width: 100px; height: 100px"/>');
		$('#thumbnail-image').val(img);
	});

	uploadImg('up-btn2',function(img){
		$('#up-container2').append('<div class="images-item"><input type="hidden" name="images[]"  value="'+img+'"><img src="'+img+'"/><a href="javascript:;" class="iclose">×</a></div>');
	});

	$('body').on('click','.images-item a',function(){
		$(this).parent().remove();
	});

	$('#item-add').click(function(){
		var html = '<tr>'+
						'<td><input type="text" class="form-control" style="width: 100px;" name="item[title][]"></td>'+
						'<td><input type="text" class="form-control" style="width: 100px;" name="item[en_title][]"></td>'+
						'<td><input type="text" class="form-control original_price" style="width: 80px;" name="item[original_price][]"></td>'+
						'<td><input type="number" class="form-control price" style="width: 80px;" name="item[price][]"></td>'+
						'<td style="text-align:center"><input type="hidden" name="item[tax][]" value="0"><i class="glyphicon glyphicon-ok tax-toggle"></i><span>0</span></td>'+
						'<td><input type="text" class="form-control" style="width: 120px;" name="item[barcode][]"></td>'+
						'<td>\
							<input type="text" name="item[stock][]" class="form-control" value="0">\
								<input type="hidden" name="item[last_stock][]" class="form-control" value="0">\
						</td>'+
						'<td>\
						<input type="text" name="item[expire][]" class="date form-control" value="<?= (new DateTime())->add(new DateInterval('P2D'))->format('Y-m-d H:i:s') ?>">\
						</td>'+
						'<td><input type="text" class="form-control" style="width: 80px;" name="item[danger_stock][]" value="5"></td>'+
						'<td><input type="text" class="form-control" style="width: 80px;" name="item[weight][]"></td>'+
						'<td class="text-center">0</td>'+
						'<td class="text-center"><a href="javascript:;" class="item-del"><?= $this->lang->line('delete') ?></a></td>'+
					'</tr>';
		$('.item-table tbody').append(html);
	})

	$('body').on('blur','.price',function(){
		var val = Math.round(parseFloat($(this).val())*1/6);
		$(this).parent().next().find('span').html(val);
	})

	$('body').on('click','.tax-toggle',function(){
		if($(this).hasClass('active')){
			$(this).removeClass('active');
			$(this).prev().val(0);
		}else{
			$(this).addClass('active');
			$(this).prev().val(1);
		}
	})

	$('body').on('click','.item-del',function(){
		if($(this).attr('item_id')){
			$.post('/admin/product/delete_item',{id:$(this).attr('item_id')});
		}
		$(this).parent().parent().remove();
	});

	$('body').on('click','.item-price-del',function(){
		$(this).parent().remove();
	});

	$('#category_id_slt').on('change',function(){
		var parent_id = $('#category_id_slt').val();
		if($('#category_id_slt').val()){
			$.getJSON('/admin/category/get_categorys/'+parent_id,function(json){
				$('#category_id2_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
				for(var i=0;i<json['data'].length;i++){
					$('#category_id2_slt').append('<option value="'+json['data'][i].id+'">'+json['data'][i].title+'</option>')
				}
			})
		}else{
			$('#category_id2_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
		}
	});

	$('input[name=product-name]').on('focus', function () {
		return $(this).trigger('input');
	});

	$('.product-recommends-btn').on('click', function () {
		var that = $('input[name=product-name]');
		var keyword = that.val();
		var page = 1;
		var pageDom = $(that).siblings('.dropdown-menu').find('.page');
		if (pageDom.length > 0) {
			page = pageDom.attr('data-id');
		}
		$.getJSON('<?= site_url('admin/product/search') ?>?keyword='+keyword+'&page='+page, function (data) {
			// 显示下拉菜单
			$(that).parents('.dropdown').addClass('open');
			var ul = $(that).siblings('.dropdown-menu');
			// 关闭下拉菜单
			$('body').on('click', function (e) {
				if ($(e.target).parents('.dropdown').length < 1) {
					$('body').off('click');
					$(that).parents('.dropdown').removeClass('open');
					ul.children().remove();
				}
			});

			var current = data.current_page;
			var products = data.data;
			// 插入产品
			// 插入页码

			ul.children().remove();
			ul.off('click');
			for (var k in products) {
				var isActive = $('input.product-'+products[k].id).length>0?'active':'';
				console.log('input.product-'+products[k].id+'=>'+$('input.product-'+products[k].id).length)
				ul.append('<li class="p-select '+isActive+'" data-id="'+products[k].id+'">'+products[k]['<?= $prefix ?>title']+'</li>');
			}
			var page = $('<li class="page" data-id="'+data.current_page+'"></li>');
			if (data.current_page > 1) {
				page.append('<span class="prev">上一页</span>');
			}
			if (data.next_page > 0) {
				page.append('<span class="next">下一页</span>');
			}
			ul.append(page);



			//翻页
			$('.prev,.next').on('click', function () {
				var now = parseInt(page.attr('data-id'));

				if ($(this).hasClass('prev')) {
					now--;
				} else {
					now++;
				}

				page.attr('data-id', now);

				$('.product-recommends-btn').trigger('click');
			});

			// 选择推荐
			ul.on('click', '.p-select', function () {
				if($(this).hasClass('active')) return;
				$(this).addClass('active');
				var id = $(this).attr('data-id');
				var recommends = $(that).parent().siblings('.recommends');
				var recommend = recommends.find('.product-'+id);
				if(recommend.length < 1) {
					var checkbox = $('<div><input type="checkbox" class="product-'+id+'" name="recommends[]" value="'+id+'" checked/> '+$(this).html()+'</div>');
					recommends.append(checkbox);
					checkbox.one('click', function () {
						$(this).remove();
					});
				} else {
					recommend.get(0).checked = true;
				}
			});
		});

	});
})



	$('.save').click(function(event) {

		if(!$('input[name=title]').val()){
			alert('<?= $this->lang->line('enter product title') ?>');
			return;
		}
		if(!$('select[name=category_id]').val()){
			alert('<?= $this->lang->line('enter product category') ?>');
			return;
		}
		if(!$('select[name=category_id2]').val()){
			alert('<?= $this->lang->line('enter product sub_category') ?>');
			return;
		}
		if(!$('#thumbnail-image').val()){
			alert("<?= $this->lang->line('please upload thumbnail') ?>");
			return;
		}
		if($('.images-item').length < 1){
			alert('<?= $this->lang->line('please upload image') ?>');
			return;
		}
		if(!ue.getContent()){
			alert('<?= $this->lang->line('enter product detail') ?>');
			return;
		}
		if($('.item-table tbody tr').length<1){
			alert('<?= $this->lang->line('miss spec') ?>');
			return;
		}
		var item_verify = true;
		$('.item-table tbody input').each(function(ele,idx){
			if(!$(this).val()) item_verify = false;
		});
		if(!item_verify){
			alert('<?= $this->lang->line('miss input') ?>');
			return;
		}

		Toast.loading();
		$.post('/admin/product/save', $('#product-form').serialize(), function(json, textStatus, xhr) {
			Toast.destory();
			if(json.code == 1){
				alert('<?= $this->lang->line('save error') ?>');
				return;
			}
			if(json.code == 2){
				alert('<?= $this->lang->line('enter error') ?>');
				return;
			}
			Toast.success('<?= $this->lang->line('success') ?>',function(){
				window.location = '<?php echo $back_url ?>#tr-'+json.product_id;
			})

		});
	});
</script>
